//
//  LandingVC.m
//  代理端
//
//  Created by sliver on 15/12/11.
//  Copyright (c) 2015年 sliver. All rights reserved.
//

#import "LandingVC.h"
#import "RegisterVC.h"
#import "HeadVC.h"
#import "Unit.h"
#import "WXDataService.h"
#import "MyMbd.h"
#import "MBProgressHUD+Add.h"
//#import "LoginMbd.h"
@interface LandingVC ()<UITextFieldDelegate>
{
    UITextField  *telTextField; //账号
    UITextField   *pwdTextField;//密码
}
@property(nonatomic,strong)UIImageView *imageview;
@property(nonatomic,strong)UIImageView *iconimage;
@end
@implementation LandingVC
-(UIImageView *)iconimage
{
    if (!_iconimage) {
//        _iconimage = [[UIImageView alloc]initWithFrame:CGRectMake(123*KScr, 150*KScr, 130*KScr, 60*KScr)];
        _iconimage = [[UIImageView alloc]initWithFrame:CGRectMake(-260, 80, 260, 118)];
        _iconimage.image = [UIImage imageNamed:@"newlogo.png"];
    }
    return _iconimage;

}
-(UIImageView *)imageview
{
    if (!_imageview) {
        _imageview= [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH)];
        _imageview.image = [UIImage imageNamed:@"beijingkaobei.png"];
        _imageview.userInteractionEnabled = YES;
        [_imageview addSubview:self.iconimage];

    }
    return _imageview;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor =DRINK_LIGHTGRAY_COLOR;
    // Do any additional setup after loading the view.
    [self loadView1];
}
-(void)loadView1{
    [self.view addSubview:self.imageview];
    [UIView animateWithDuration:1 animations:^{
        self.iconimage.frame = CGRectMake(100, 80, 260, 118);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.5 animations:^{
            self.iconimage.frame =CGRectMake(57.5*KScr, 80*KScr, 260*KScr, 118*KScr);
            
        } completion:^(BOOL finished) {
            
        }];
    }];

    //第一个输入框view
    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(20, 245,ScreenW - 40, 45)];
//    view1.backgroundColor = [UIColor whiteColor];
    [view1.layer setBorderWidth:1];
    [view1.layer setBorderColor:[UIColor whiteColor].CGColor];

    view1.layer.masksToBounds = YES;
    view1.layer.cornerRadius = 5;
    [self.view addSubview:view1];
    //图
    UIImageView *imgview = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 20, 20)];
    imgview.image = [UIImage imageNamed:@"iconfont-qiye.png"];
    [view1 addSubview:imgview];
    //输入框
    //输入手机号TextField
    telTextField = [[UITextField alloc]initWithFrame:CGRectMake(40, 5, ScreenW-120, 35)];
    telTextField.delegate = self;
    telTextField.textColor = [UIColor whiteColor];
    telTextField.font = [UIFont systemFontOfSize:18.0];
    telTextField.placeholder = @"请输入公司简称";
    telTextField.textAlignment = NSTextAlignmentLeft;
    [telTextField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
    telTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    telTextField.keyboardType = UIKeyboardTypeDefault;
    telTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    telTextField.returnKeyType = UIReturnKeyDone;
    if([FLAG integerValue] == 1){
        telTextField.text = USER_ID;
    }
    [view1 addSubview:telTextField];

    //第二个输入框
    UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(20, 300,ScreenW - 40, 45)];
//    view2.backgroundColor = [UIColor whiteColor];
    [view2.layer setBorderWidth:1];
    [view2.layer setBorderColor:[UIColor whiteColor].CGColor];
    view2.layer.masksToBounds = YES;
    view2.layer.cornerRadius = 5;
    [self.view addSubview:view2];
    //图
    UIImageView *imgview2 = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 20, 20)];
    imgview2.image = [UIImage imageNamed:@"iconfont-mima-(2).png"];
    [view2 addSubview:imgview2];
    //输入密码TextField
    pwdTextField = [[UITextField alloc]initWithFrame:CGRectMake(40, 5, ScreenW-80, 35)];
    pwdTextField.delegate = self;
    pwdTextField.textColor = [UIColor whiteColor];
    pwdTextField.placeholder = @"请输入密码";
    pwdTextField.secureTextEntry = YES;
    [pwdTextField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
    pwdTextField.font = [UIFont systemFontOfSize:18];
    pwdTextField.textAlignment = NSTextAlignmentLeft;
    pwdTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    pwdTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    pwdTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    pwdTextField.returnKeyType = UIReturnKeyDone;
    if ([FLAG integerValue] == 1) {
        pwdTextField.text = USER_PWD;
    }
    [view2 addSubview:pwdTextField];
    //记住密码
//    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(40, 245, 20, 20)];
//    [btn setImage:[UIImage imageNamed:@"圆角"] forState:UIControlStateNormal];
//    [btn setImage:[UIImage imageNamed:@"圆角勾号"] forState:UIControlStateSelected];
//    if ([FLAG integerValue] == 1) {
//        btn.selected = YES;
//    }
//    [btn addTarget:self action:@selector(jizhumima:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:btn];
//    UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(65, 245, 60, 20)];
//    lab.text = @"记住密码";
//    lab.textColor = [UIColor grayColor];
//    lab.font = [UIFont systemFontOfSize:13];
//    [self.view addSubview:lab];
    //注册
    
    UIImageView *imageview= [[UIImageView alloc]initWithFrame:CGRectMake(ScreenW-30, 353, 7, 14)];
    imageview.image = [UIImage imageNamed:@"btn_jiantou_you.png"];
    [self.view addSubview:imageview];
    
    UIButton *registerBtn = [[UIButton alloc]initWithFrame:CGRectMake(ScreenW - 130, 350, 100, 20)];
//    registerBtn.backgroundColor = DRINK_LIGHTGRAY_COLOR;
    [registerBtn setBackgroundColor:[UIColor clearColor]];
    [registerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [registerBtn setTitle:@"申请成为代理商" forState:UIControlStateNormal];
    [registerBtn addTarget:self action:@selector(clickregister:) forControlEvents:UIControlEventTouchUpInside];
    registerBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.view addSubview:registerBtn];
    //登陆按钮
    UIButton *landBtn = [[UIButton alloc]initWithFrame:CGRectMake(20, ScreenH - 100, ScreenW - 40, 50)];
    landBtn.backgroundColor = [UIColor whiteColor];
    landBtn.layer.masksToBounds = YES;
    landBtn.layer.cornerRadius = 5;
    [landBtn setTitle:@"登录" forState:UIControlStateNormal];
    [landBtn setTitleColor:DRINK_RED_COLOR forState:UIControlStateNormal];
    landBtn.titleLabel.font = [UIFont boldSystemFontOfSize:25];
    [landBtn addTarget:self action:@selector(clickLand:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:landBtn];
    NSUserDefaults *userDefaultes = [NSUserDefaults standardUserDefaults];
    [userDefaultes setObject:@(1) forKey:@"FLAG"];
    [userDefaultes synchronize];

}
#pragma mark - 记住密码
//-(void)jizhumima:(UIButton *)sender{
//    
//    sender.selected = !sender.selected;
//    if(sender.selected == YES){
//    //保存用户信息
//    NSUserDefaults *userDefaultes = [NSUserDefaults standardUserDefaults];
//    [userDefaultes setObject:@(1) forKey:@"FLAG"];
//    [userDefaultes synchronize];
//    }else{
//        //保存用户信息
//        NSUserDefaults *userDefaultes = [NSUserDefaults standardUserDefaults];
//        [userDefaultes setObject:@(-1) forKey:@"FLAG"];
//        [userDefaultes synchronize];
//    }
//}
#pragma mark - 点击登陆
-(void)clickLand:(UIButton *)sender{
    
    if (telTextField.text.length>0&&pwdTextField.text.length>0) {
        [self postNetWorking];
    }else
    {
        UIAlertView *deleAlertView=[[UIAlertView alloc]initWithTitle:nil message:@"请输入账号或密码" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [deleAlertView show];
    }



}
#pragma mark--- 发送请求的代码
-(void)postNetWorking
{
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:telTextField.text forKey:@"account"];
    [params setObject:pwdTextField.text forKey:@"login_pass"];
    [MyMbd addHudToView:self.view];
    [WXDataService requestWithURL:@"mobile/agency/login.jhtml" params:params httpMethod:@"POST" block:^(id result, NSString *success, NSString *msg) {
        [MyMbd hidenHudFromView:self.view];
        BOOL suc =[success boolValue];
        if (suc == 0 ) {
            [BackView addToview:self.view WithDeleget:self WithMessage:msg WithBtnCount:1];
//            UIAlertView *deleAlertView=[[UIAlertView alloc]initWithTitle:nil message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
//            [deleAlertView show];
            return;
        }else if(suc == 1){
            NSUserDefaults *userDefaultes = [NSUserDefaults standardUserDefaults];
            if([FLAG integerValue] == 1){
//                NSLog(@"%@",result);
                //保存用户信息
                [userDefaultes setObject:telTextField.text forKey:@"USER_ID"];
                [userDefaultes setObject:pwdTextField.text forKey:@"USER_PWD"];
                [userDefaultes setObject:result[@"agencyPO"][@"avatar"] forKey:@"USER_IMG"];
                [userDefaultes setObject:result[@"agencyPO"][@"level"] forKey:@"USER_LEVEL"];
                [userDefaultes setObject:result[@"agencyPO"][@"id_"] forKey:@"USER_ACCOUNT"];
                [userDefaultes setObject:result[@"agencyPO"] forKey:@"USER_DIC"];
                [userDefaultes setObject:result[@"agencyPO"][@"company_name"] forKey:@"CPOMANY_NAME"];
                [userDefaultes synchronize];
                
                
//                [userDefaultes objectForKey:@"USER_PWD"]
                
            }
            NSString *tuichudenglu = [NSString stringWithFormat:@"%@", [userDefaultes objectForKey:@"tuichudenglu"]];
            if ([tuichudenglu integerValue] == 1) {
                [self dismissViewControllerAnimated:YES completion:nil];
                [userDefaultes removeObjectForKey:@"tuichudenglu"];
            }else{
                HeadVC *headvc = [[HeadVC alloc]init];
                headvc.modalTransitionStyle =     UIModalTransitionStyleFlipHorizontal;
                [self presentViewController:headvc animated:YES completion:nil];
            }
        }
    }
                        failblock:^(AFHTTPRequestOperation *operation, NSError *error) {
                            [MyMbd hidenHudFromView:self.view];
                            [MBProgressHUD showMessage:@"链接服务器失败.." view:self.view];
                            
                        }
     
     ];



}
#pragma mark - 点击注册
-(void)clickregister:(UIButton *)sender{
    RegisterVC *registervc = [[RegisterVC alloc]init];
    registervc.modalTransitionStyle =  UIModalTransitionStyleCoverVertical;
    [self presentViewController:registervc animated:YES completion:nil];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];
    
    return YES;
}
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    [telTextField resignFirstResponder];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
